package com.sjsu.CMPE226.etl.dataloader;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sjsu.CMPE226.Hibernate.config.NormalisedHibernateUtil;
import com.sjsu.CMPE226.etl.beans.Network;
import com.sjsu.CMPE226.etl.beans.Observation;
import com.sjsu.CMPE226.etl.beans.Provider;
import com.sjsu.CMPE226.etl.beans.Station;

/**
 * 
 * @author Pawar Deepali
 * 
 */
public class NormalisedORMDataLoader {
	public int loadObservationsBatch(List<Observation> observationList){
		long st = System.currentTimeMillis();

		Session session = NormalisedHibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			for (Observation observation : observationList) {
				if (observation.getDatetime() == null) {

				} else {
					session.save(observation);
					if (observationList.size() % 1000 == 0) {
						session.flush();
						session.clear();						
					}
				}
			}
			tx.commit();
			long et = System.currentTimeMillis();
			System.out
					.println("Time to load data through ORM in normalised DB in 'Observation' Table:"
							+ (et - st) + " msec");
		} catch(Exception e){
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
		finally {
			session.close();
		}
		return 1;
	}

	public int loadStationsBatch(List<Station> stationList) {
		long st = System.currentTimeMillis();

		Session session = NormalisedHibernateUtil.getSessionFactory().getCurrentSession();
		try {
			Transaction tx = session.beginTransaction();
			for (Station station : stationList) {
				session.save(station);
				if (stationList.size() % 1000 == 0) {
					session.flush();
					session.clear();
					tx.commit();
				}

			}
			tx.commit();
			long et = System.currentTimeMillis();
			System.out
					.println("Time to load data through ORM in normalised DB in 'Station' Table:"
							+ (et - st) + " msec");
		} catch(Exception e){
			
		}
		finally {
			// session.close();
		}
		return 1;
	}

	public int loadNetworksBatch(List<Network> networkList) {
		long st = System.currentTimeMillis();

		Session session = NormalisedHibernateUtil.getSessionFactory().getCurrentSession();
		try {
			Transaction tx = session.beginTransaction();
			for (Network network : networkList) {
				session.save(network);
				if (networkList.size() % 1000 == 0) {
					session.flush();
					session.clear();
					tx.commit();
				}

			}
			tx.commit();
			long et = System.currentTimeMillis();
			System.out
					.println("Time to load data through ORM in normalised DB in 'Network' Table:"
							+ (et - st) + " msec");
		} catch(Exception e){
			
		}finally {
			// session.close();
		}
		return 1;
	}

	public int loadProvidersBatch(List<Provider> providerList) {
		long st = System.currentTimeMillis();

		Session session = NormalisedHibernateUtil.getSessionFactory().getCurrentSession();
		try {
			Transaction tx = session.beginTransaction();
			for (Provider provider : providerList) {
				session.save(provider);
				if (providerList.size() % 1000 == 0) {
					session.flush();
					session.clear();
					tx.commit();
				}
			}
			tx.commit();
			long et = System.currentTimeMillis();
			System.out
					.println("Time to load data through ORM in normalised DB in 'Provider' Table :"
							+ (et - st) + " msec");
		} catch(Exception e){
			
		}finally {
			// session.close();
		}
		return 1;
	}

}
